﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HiLand.Utility.Data
{
    public static class SafeString
    {
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="input"></param>
        /// <param name="remainCharCount"></param>
        /// <returns></returns>
        public static string SubString(string input, int remainCharCount)
        {
            return SubString(input, remainCharCount, false);
        }

        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="input"></param>
        /// <param name="remainCharCount"></param>
        /// <param name="isAddPostFix">是否在截取后的字符串后加入一个省略号</param>
        /// <returns></returns>
        public static string SubString(string input, int remainCharCount,bool isAddPostFix)
        { 
            string result= string.Empty;
            if (input.Length > remainCharCount)
            {
                result = input.Substring(0,remainCharCount);
                if (isAddPostFix == true)
                {
                    result += "...";
                }
            }
            else
            {
                result = input;
            }

            return result;
        }

        /// <summary>
        /// 格式化要显示的内容，主要用于在网页上显示由textarea产生的内容
        /// </summary>
        /// <param name="value">传入字符串</param>
        /// <returns></returns>
        public static string FormatHtml(string value)
        {
            if (value.Trim().Length == 0)
            {
                return string.Empty;
            }

            value = value.Replace("\r\n", "<br/>");
            value = value.Replace("\n", "<br/>");

            return value;
        }
    }
}
